package com.jch.eduservice.service.impl;

import com.jch.eduservice.entity.EduCourse;
import com.jch.eduservice.entity.EduCourseDescription;
import com.jch.eduservice.entity.vo.CourseInfoVo;
import com.jch.eduservice.mapper.EduCourseMapper;
import com.jch.eduservice.service.EduCourseDescriptionService;
import com.jch.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jch.servicebase.exceptionhandler.JchException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author jch
 * @since 2024-12-21
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {

        //向课程表添加课程基本信息
        EduCourse eduCourse = new EduCourse();
        //courseInfoVo转换为eduCourse
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert==0){
            //添加课程信息失败
            throw new JchException(20001,"添加课程信息失败");
        }
        //向课程简介表添加课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        //courseInfoVo转换为eduCourseDescription
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(eduCourse.getId());
        eduCourseDescriptionService.save(eduCourseDescription);
        return eduCourse.getId();
    }
}
